package com.example.qing.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.qing.config.AuthAccess;
import com.example.qing.config.LogType;
import com.example.qing.config.MyLogs;
import com.example.qing.config.Views;
import com.example.qing.pojo.Result;

import com.example.qing.pojo.User;
import com.example.qing.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Resource
    UserService userService;

    @GetMapping
    public Result list() {
        log.info("查询用户信息");
        QueryWrapper<User>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role","用户");
        List<User>userList = userService.list(queryWrapper);

        return Result.success(userList);
    }


    @GetMapping("/manger")
    public Result managerList() {
        QueryWrapper<User>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role","管理员");
        List<User>userList = userService.list(queryWrapper);
        return Result.success(userList);
    }

    @AuthAccess
    @GetMapping("/super")
      public Result getOne(){
        QueryWrapper<User> queryWrapper =new QueryWrapper<User>();
        queryWrapper.eq("role","超级管理员");
        User user=userService.getOne(queryWrapper);
        return Result.success(user);
    }


    @GetMapping("/total")
    public Result total(){
        QueryWrapper<User>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role","用户");
        long count=userService.count(queryWrapper);
        return Result.success(count);
    }

    @MyLogs(operation = "用户",type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        userService.removeById(id);
        return Result.success();
    }
    @MyLogs(operation = "管理员",type = LogType.BATCH_DELETE)
    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<Integer> ids){
        log.info("批量删除");
        userService.removeByIds(ids);
        return Result.success();
    }
    @MyLogs(operation = "管理员",type = LogType.ADD)
    @PostMapping("/add")
    public Result save(@RequestBody User user){
        log.info("新增用户");
        userService.save(user);
        return Result.success();
    }

    @AuthAccess
    @MyLogs(operation = "用户",type = LogType.ADD)
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        QueryWrapper queryWrapper=new QueryWrapper<User>();
        queryWrapper.eq("username",user.getUsername());
        User temp= userService.getOne(queryWrapper);
        if(temp!=null){
            return Result.error("账号已存在");
        }
        userService.save(user);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){

        User user=userService.getById(id);
        return Result.success(user);
    }
    @MyLogs(operation = "用户",type = LogType.UPDATE)
    @PutMapping("/update")
    public Result update(@RequestBody User user){
        userService.updateById(user);
        return Result.success();
    }


    @GetMapping("/selectByPage")
    public Result list(@RequestParam Integer pageNum,
                       @RequestParam Integer pageSize,
                       @RequestParam String username,
                       @RequestParam String name){
        log.info("分页查询");
        QueryWrapper<User>queryWrapper=new QueryWrapper<User>().orderByDesc("id");
        queryWrapper.like(StrUtil.isNotBlank(username),"username",username);
        queryWrapper.like(StrUtil.isNotBlank(username),"name",name);
        queryWrapper.eq("role","用户");
        Page<User>page=userService.page(new Page<>(pageNum,pageSize),queryWrapper);
        return Result.success(page);
    }

    @GetMapping("/selectByPage/admin")
    public Result adminList(@RequestParam Integer pageNum,
                       @RequestParam Integer pageSize,
                       @RequestParam String username,
                       @RequestParam String name) {
        log.info("分页查询");
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>().orderByDesc("id");
        queryWrapper.like(StrUtil.isNotBlank(username), "username", username);
        queryWrapper.like(StrUtil.isNotBlank(username), "name", name);
        queryWrapper.eq("role", "管理员");
        Page<User> page = userService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }
    @GetMapping("/views/count")
    public Result views(){
        String views=(String) redisTemplate.opsForValue().get("views");
        return  Result.success(views);
    }


}